from collections import Iterable


def iterator(x):
    if not isinstance(x, int):
        print("type is error")
        return
    total = 0
    while x > 0:
        total += x
        x -= 1
    print(total)


iterator(9)

s = [1, 2, 3, 4, 5, 56, 3, 2, 2, 2, 2, 2, 2, 2, 2]


def for_iterator(s):
    total = 0
    for n in s:
        total += n
    print(total)


for_iterator(s)


# for_iterator(2)   'int' object is not iterable


def for_iterator_dict(x):
    if not isinstance(x, Iterable):
        print("param can not iterator, request param is -->", x)
        return
    for key in x:
        print(key)
    for k, v in x.items():
        print(k, v)


d = {'a': 1, 'b': 2, 'c': 3}
for_iterator_dict(d)
for_iterator_dict(3)

for x, y in [(1, 1), (2, 4), (3, 9)]:
    print(x, y)

# 列表生成式
l = list(range(1, 11))
print(l)

# 复杂列表生成式
l2 = [x * x for x in range(1, 11) if x % 2 == 0]
print(l2)

# 复杂列表生成式，两层循环可以生成全排列
l3 = [m + n for m in 'AA' for n in 'BB']
print(l3)


